package com.javaxiaobear.module.monitor.mapper;

import static com.javaxiaobear.module.monitor.domain.table.SysJobLogTableDef.SYS_JOB_LOG;

import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.monitor.domain.SysJobLog;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;
import org.apache.ibatis.annotations.Delete;

/**
 * 调度任务日志信息 数据层
 *
 * @author javaxiaobear
 */
public interface SysJobLogMapper extends BaseMapper<SysJobLog> {

  /**
   * 获取quartz调度器日志的计划任务
   *
   * @param jobLog 调度日志信息
   * @return 调度任务日志集合
   */
  default List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (jobLog.getJobName() != null && !jobLog.getJobName().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.JOB_NAME.like(jobLog.getJobName()));
    }

    if (jobLog.getJobGroup() != null && !jobLog.getJobGroup().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.JOB_GROUP.eq(jobLog.getJobGroup()));
    }

    if (jobLog.getStatus() != null) {
      queryWrapper.and(SYS_JOB_LOG.STATUS.eq(jobLog.getStatus()));
    }

    if (jobLog.getInvokeTarget() != null && !jobLog.getInvokeTarget().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.INVOKE_TARGET.like(jobLog.getInvokeTarget()));
    }

    // 时间范围查询
    if (jobLog.getParams() != null) {
      if (jobLog.getParams().get("beginTime") != null) {
        queryWrapper.and(SYS_JOB_LOG.CREATE_TIME.ge(jobLog.getParams().get("beginTime")));
      }

      if (jobLog.getParams().get("endTime") != null) {
        queryWrapper.and(SYS_JOB_LOG.CREATE_TIME.le(jobLog.getParams().get("endTime")));
      }
    }

    queryWrapper.orderBy(SYS_JOB_LOG.CREATE_TIME.desc());
    return selectListByQuery(queryWrapper);
  }

  default Page<SysJobLog> selectJobLogPage(SysJobLog jobLog) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (jobLog.getJobName() != null && !jobLog.getJobName().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.JOB_NAME.like(jobLog.getJobName()));
    }

    if (jobLog.getJobGroup() != null && !jobLog.getJobGroup().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.JOB_GROUP.eq(jobLog.getJobGroup()));
    }

    if (jobLog.getStatus() != null) {
      queryWrapper.and(SYS_JOB_LOG.STATUS.eq(jobLog.getStatus()));
    }

    if (jobLog.getInvokeTarget() != null && !jobLog.getInvokeTarget().isEmpty()) {
      queryWrapper.and(SYS_JOB_LOG.INVOKE_TARGET.like(jobLog.getInvokeTarget()));
    }

    // 时间范围查询
    if (jobLog.getParams().get("beginTime") != null) {
      queryWrapper.and(SYS_JOB_LOG.CREATE_TIME.ge(jobLog.getParams().get("beginTime")));
    }

    if (jobLog.getParams().get("endTime") != null) {
      queryWrapper.and(SYS_JOB_LOG.CREATE_TIME.le(jobLog.getParams().get("endTime")));
    }

    queryWrapper.orderBy(SYS_JOB_LOG.CREATE_TIME.desc());
    return paginate(PageUtils.getPage(queryWrapper), queryWrapper);
  }

  /**
   * 查询所有调度任务日志
   *
   * @return 调度任务日志列表
   */
  default List<SysJobLog> selectJobLogAll() {
    return selectAll();
  }

  /**
   * 通过调度任务日志ID查询调度信息
   *
   * @param jobLogId 调度任务日志ID
   * @return 调度任务日志对象信息
   */
  default SysJobLog selectJobLogById(Long jobLogId) {
    return selectOneById(jobLogId);
  }

  /**
   * 新增任务日志
   *
   * @param jobLog 调度日志信息
   * @return 结果
   */
  default int insertJobLog(SysJobLog jobLog) {
    return insert(jobLog);
  }

  /**
   * 批量删除调度日志信息
   *
   * @param logIds 需要删除的数据ID
   * @return 结果
   */
  default int deleteJobLogByIds(Long[] logIds) {
    if (logIds == null || logIds.length == 0) {
      return 0;
    }
    return deleteBatchByIds(Arrays.asList(logIds));
  }

  /**
   * 删除任务日志
   *
   * @param jobId 调度日志ID
   * @return 结果
   */
  default int deleteJobLogById(Long jobId) {
    return deleteById(jobId);
  }

  /** 清空任务日志 */
  @Delete("truncate table sys_job_log")
  void cleanJobLog();
}
